using System.Collections.Generic;
using Hope;
using DataFramework;
using System;
using UnityEngine;

public abstract class BasePlayerEventHandle
{
    protected EventHandleData arg;
    protected int priority;
    protected bool bDeInit = false;
    public void Init(EventHandleData argconfig, int priority = 0)
    {
        this.priority = priority;
        this.arg = argconfig;
        _InitArgs();
        _Init();
        registerEvent();
    }

    protected virtual void _InitArgs()
    {

    }

    protected virtual void _Init()
    {

    }

    protected void registerEvent()
    {
        PlayerEventCenter.AddEventHandle(arg.eventName, OnHit, priority);
    }

    protected void unRegiterEvent()
    {
        PlayerEventCenter.RemoveEventHandle(arg.eventName, OnHit);
    }

    protected virtual void OnHit(EventHandleArgs arg)
    {
        if (CheckConditionsReached(arg))
        {
            Excute();
            DeInit();
        }
    }

    public void DeInit()
    {
        if (bDeInit)
        {
            return;
        }
        bDeInit = true;
        unRegiterEvent();
        _DeInit();
    }

    protected virtual void _DeInit()
    {

    }
    /// <summary>
    /// 检测条件是否达成
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    public abstract bool CheckConditionsReached(EventHandleArgs arg);

    /// <summary>
    /// 条件达成, 开始执行
    /// </summary>
    public virtual void Excute()
    {
        Debug.Log("excute");
        arg.Excute();
    }

    public T Conver<T>(object arg)
    {
        var type = typeof(T);
        if (type.BaseType != typeof(ValueType) && type != typeof(string))
        {
            return default(T);
        }
        return (T)Convert.ChangeType(arg, typeof(T));
    }
}

/// <summary>
/// 游戏事件类型
/// </summary>
public abstract class PlayerEventHandle<Arg1, Arg2, Arg3, Arg4> : BasePlayerEventHandle
{

    public virtual Arg1 arg1 {
        get;
        set;
    }

    public virtual Arg2 arg2 {
        get;
        set;
    }

    public virtual Arg3 arg3 {
        get;
        set;
    }

    public virtual Arg4 arg4 {
        get;
        set;
    }


    protected override void _InitArgs()
    { 
        arg1 = Conver<Arg1>(arg.arg1);
        arg2 = Conver<Arg2>(arg.arg2);
        arg3 = Conver<Arg3>(arg.arg3);
        arg4 = Conver<Arg4>(arg.arg4);
    }
}

/// <summary>
/// 游戏事件类型
/// </summary>
public abstract class PlayerEventHandle<Arg1, Arg2, Arg3> : BasePlayerEventHandle
{
    public virtual Arg1 arg1 {
        get;
        set;
    }

    public virtual Arg2 arg2 {
        get;
        set;
    }

    public virtual Arg3 arg3 {
        get;
        set;
    }

    protected override void _InitArgs()
    {
        arg1 = Conver<Arg1>(arg.arg1);
        arg2 = Conver<Arg2>(arg.arg2);
        arg3 = Conver<Arg3>(arg.arg3);
    }
}

/// <summary>
/// 游戏事件类型
/// </summary>
public abstract class PlayerEventHandle<Arg1, Arg2> : BasePlayerEventHandle
{
    public virtual Arg1 arg1 {
        get;
        set;
    }

    public virtual Arg2 arg2 {
        get;
        set;
    }


    protected override void _InitArgs()
    {
        arg1 = Conver<Arg1>(arg.arg1);
        arg2 = Conver<Arg2>(arg.arg2);
    }
}

/// <summary>
/// 游戏事件类型
/// </summary>
public abstract class PlayerEventHandle<Arg1> : BasePlayerEventHandle
{

    public virtual Arg1 arg1 {
        get;
        set;
    }

    protected override void _InitArgs()
    {
        arg1 = Conver<Arg1>(arg.arg1);
    }
}

/// <summary>
/// 游戏事件类型
/// </summary>
public abstract class PlayerEventHandle : BasePlayerEventHandle
{
    TutorialEventConfig config;
}